package com.useeinfo.oa.modules.base.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.entity.SuperClassEntity;
import com.useeinfo.framework.sugar.tools.CommonSugar;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.util.*;

/**
 * @Auther: xudong.sun
 * @Date: 2018/11/30 10:21
 * @Description: 设备表
 */
@Entity
@Table(name = "facility")
public class Facility extends SuperClassEntity<Facility> {

    /**
     * 序列号
     */
    private String serialNum;

    /**
     * 流水号(自动生成)
     */
    private String serialCode;

    /**
     * 设备编号
     */
    private String code;

    /**
     * 设备名称
     */
    private String name;
    /**
     * 设备是否停用（默认为false 即启用）
     */
    @Column(columnDefinition = "bit default 0")
    private boolean stop;
    /**
     * 设备是否异常默认为false 即正常）
     */
    @Column(columnDefinition = "bit default 0")
    private boolean abnormal;

    /**
     * 生产厂家
     */
    private String factory;

    /**
     * 位置
     */
    private String position;

    /**
     * 具体位置
     */
    private String precisePosition;
    /**
     * 详细位置
     */
    private String detailPosition;
    /**
     * 经度
     */
    private Double longitude ;
    /**
     * 纬度
     */
    private Double latitude;
    /**
     * TPM指导书
     */
    private String tpmBook;
    /**
     * 设备重要等级A B C
     */
    private String importantDegree;
    /**
     * 功率
     */
    private String power;

    /**
     * 功能/用途
     */
    @Column(columnDefinition="text")
    private String purpose;

    /**
     * 主要技术参数
     */
    @Column(columnDefinition="text")
    private String mainParm;

    /**
     * 出厂日期
     */
    @Temporal(TemporalType.DATE)
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date releaseDate;

    /**
     * 启用日期
     */
    @Temporal(TemporalType.DATE)
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date enableDate;

    /**
     * 保用期
     */
    private String guarantee;

    /**
     * 是否高能
     */
    @Column(columnDefinition = "bit default 0")
    private boolean highEnergy;

    /**
     * 是否室内
     */
    @Column(columnDefinition = "bit default 0")
    private boolean indoor;

    /**
     * 是否有效
     */
    @Column(columnDefinition = "bit default 0")
    private boolean effective;

    /**
     * 上级电源
     */
    private String upPower;

    /**
     * 备用电源
     */
    private String sparePower;

    /**
     * 所属系统
     */
    private String belongSystem;

    /**
     * 所属子系统
     */
    private String subSystem;

    /**
     * 所属主设备
     */
    private Long parentId;
    /**
     * 所属主设备名称
     */
    private String parentName;

    /**
     * 二维码值
     */
    private String qrCode;

    /**
     * 固定资产编号
     */
    private String faCode;

    /**
     * 规格型号
     */
    private String type;

    /**
     * 成本中心
     */
    private String costCenter;

    /**
     * 网格化责任人
     */
    private String gridPerson;
    /**
     * 现场照片
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "field_photo", joinColumns = {
            @JoinColumn(name = "facility_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "resource_id", referencedColumnName = "id")})
    private Set<Resource> fieldPhoto = new HashSet<Resource>();

    /**
     * 铭牌照片
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "plate_photo", joinColumns = {
            @JoinColumn(name = "facility_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "resource_id", referencedColumnName = "id")})
    private Set<Resource> platePhoto = new HashSet<Resource>();


    /**
     * 所属分组
     */
    @ManyToOne
    @JoinColumn(name = "facilityGroup_id")
    private FacilityGroup facilityGroup;

    /**
     * 所属区域
     */
    @ManyToOne
    @JoinColumn(name = "area_id")
    private Area area;

    /**
     * 所属类型
     */
    @ManyToOne
    @JoinColumn(name = "facilityCat_Id")
    private FacilityCat facilityCat;

    /**
     * 所属部门
     */
    @ManyToOne
    @JoinColumn(name = "organization_id")
    private Organization organization;

    /**
     * 所属企业
     */
    @ManyToOne
    @JoinColumn(name = "enterprise_id")
    private Enterprise enterprise;

    /**
     * 所属专业ID
     */
    private Long rootCatId;


    public String getSerialNum() {
        return serialNum;
    }

    public void setSerialNum(String serialNum) {
        this.serialNum = serialNum;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFactory() {
        return factory;
    }

    public void setFactory(String factory) {
        this.factory = factory;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public String getPrecisePosition() {
        return precisePosition;
    }

    public void setPrecisePosition(String precisePosition) {
        this.precisePosition = precisePosition;
    }

    public String getPurpose() {
        return purpose;
    }

    public void setPurpose(String purpose) {
        this.purpose = purpose;
    }

    public String getMainParm() {
        return mainParm;
    }

    public void setMainParm(String mainParm) {
        this.mainParm = mainParm;
    }

    public Date getReleaseDate() {
        return releaseDate;
    }

    public void setReleaseDate(Date releaseDate) {
        this.releaseDate = releaseDate;
    }

    public Date getEnableDate() {
        return enableDate;
    }

    public void setEnableDate(Date enableDate) {
        this.enableDate = enableDate;
    }

    public String getGuarantee() {
        return guarantee;
    }

    public void setGuarantee(String guarantee) {
        this.guarantee = guarantee;
    }

    public Boolean getHighEnergy() {
        return highEnergy;
    }

    public void setHighEnergy(Boolean highEnergy) {
        this.highEnergy = highEnergy;
    }

    public Boolean getIndoor() {
        return indoor;
    }

    public void setIndoor(Boolean indoor) {
        this.indoor = indoor;
    }

    public Boolean getEffective() {
        return effective;
    }

    public void setEffective(Boolean effective) {
        this.effective = effective;
    }

    public String getUpPower() {
        return upPower;
    }

    public void setUpPower(String upPower) {
        this.upPower = upPower;
    }

    public String getSparePower() {
        return sparePower;
    }

    public void setSparePower(String sparePower) {
        this.sparePower = sparePower;
    }

    public String getBelongSystem() {
        return belongSystem;
    }

    public void setBelongSystem(String belongSystem) {
        this.belongSystem = belongSystem;
    }

    public String getSubSystem() {
        return subSystem;
    }

    public void setSubSystem(String subSystem) {
        this.subSystem = subSystem;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public String getQrCode() {
        return qrCode;
    }

    public void setQrCode(String qrCode) {
        this.qrCode = qrCode;
    }

    public String getFaCode() {
        return faCode;
    }

    public void setFaCode(String faCode) {
        this.faCode = faCode;
    }

    public Set<Resource> getFieldPhoto() {
        return fieldPhoto;
    }

    public void setFieldPhoto(Set<Resource> fieldPhoto) {
        this.fieldPhoto = fieldPhoto;
    }

    public Set<Resource> getPlatePhoto() {
        return platePhoto;
    }

    public void setPlatePhoto(Set<Resource> platePhoto) {
        this.platePhoto = platePhoto;
    }

    public FacilityGroup getFacilityGroup() {
        return facilityGroup;
    }

    public void setFacilityGroup(FacilityGroup facilityGroup) {
        this.facilityGroup = facilityGroup;
    }

    public Area getArea() {
        return area;
    }

    public void setArea(Area area) {
        this.area = area;
    }

    public FacilityCat getFacilityCat() {
        return facilityCat;
    }

    public void setFacilityCat(FacilityCat facilityCat) {
        this.facilityCat = facilityCat;
    }

    public Organization getOrganization() {
        return organization;
    }

    public void setOrganization(Organization organization) {
        this.organization = organization;
    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    public String getCostCenter() {
        return costCenter;
    }

    public void setCostCenter(String costCenter) {
        this.costCenter = costCenter;
    }

    public String getGridPerson() {
        return gridPerson;
    }

    public void setGridPerson(String gridPerson) {
        this.gridPerson = gridPerson;
    }

    public Double getLongitude() {
        return longitude;
    }

    public void setLongitude(Double longitude) {
        this.longitude = longitude;
    }

    public Double getLatitude() {
        return latitude;
    }

    public void setLatitude(Double latitude) {
        this.latitude = latitude;
    }

    public String getTpmBook() {
        return tpmBook;
    }

    public void setTpmBook(String tpmBook) {
        this.tpmBook = tpmBook;
    }

    public String getSerialCode() {
        return serialCode;
    }

    public void setSerialCode(String serialCode) {
        this.serialCode = serialCode;
    }

    public String getParentName() {
        return parentName;
    }

    public void setParentName(String parentName) {
        this.parentName = parentName;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDetailPosition() {
        return detailPosition;
    }

    public void setDetailPosition(String detailPosition) {
        this.detailPosition = detailPosition;
    }

    public String getImportantDegree() {
        return importantDegree;
    }

    public void setImportantDegree(String importantDegree) {
        this.importantDegree = importantDegree;
    }

    public String getPower() {
        return power;
    }

    public void setPower(String power) {
        this.power = power;
    }

    public boolean isStop() {
        return stop;
    }

    public void setStop(boolean stop) {
        this.stop = stop;
    }

    public boolean isAbnormal() {
        return abnormal;
    }

    public void setAbnormal(boolean abnormal) {
        this.abnormal = abnormal;
    }

    public Long getRootCatId() {
        return rootCatId;
    }

    public void setRootCatId(Long rootCatId) {
        this.rootCatId = rootCatId;
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject jsonObject = super.toJSONObject();
        jsonObject.put("serialNum", StringConverters.null2String(getSerialNum()));
        jsonObject.put("serialCode", StringConverters.null2String(getSerialCode()));
        jsonObject.put("name", StringConverters.null2String(getName()));
        jsonObject.put("code", StringConverters.null2String(getCode()));
        jsonObject.put("factory", StringConverters.null2String(getFactory()));
        jsonObject.put("type", StringConverters.null2String(getType()));
        jsonObject.put("parentName", StringConverters.null2String(getParentName()));
        jsonObject.put("position", StringConverters.null2String(getPosition()));
        jsonObject.put("precisePosition", StringConverters.null2String(getPrecisePosition()));
        jsonObject.put("detailPosition", StringConverters.null2String(getDetailPosition()));
        jsonObject.put("facilityCatName", getFacilityCat() == null ? "" : getFacilityCat().getCatName());
        jsonObject.put("facilityCatId", getFacilityCat() == null ? null : getFacilityCat().getId());
        jsonObject.put("facilityGroupName", getFacilityGroup() == null ? "" : getFacilityGroup().getFacilityGroupName());
        jsonObject.put("areaName", getArea() == null ? "" : getArea().getAreaName());
        jsonObject.put("purpose", StringConverters.null2String(getPurpose()));
        jsonObject.put("mainParm", StringConverters.null2String(getMainParm()));
        jsonObject.put("releaseDate", getReleaseDate());
        //1.14	规格型号
        // jsonObject.put("",StringConverters.null2String());
        //1.15	功率
        jsonObject.put("upPower", StringConverters.null2String(getUpPower()));
        //  jsonObject.put("",StringConverters.null2String());
        if(getEnterprise()!=null){
            jsonObject.put("enterpriseName", StringConverters.null2String(getEnterprise().getEnterpriseName()));
        }else{
            jsonObject.put("enterpriseName", "");
        }
        jsonObject.put("isStop", isStop());

        return jsonObject;
    }


    public JSONObject toDetailJSONObject() {
        JSONObject jsonObject = super.toJSONObject();
        jsonObject.put("facilityId",StringConverters.null2Long(getId()));
        jsonObject.put("name", StringConverters.null2String(getName()));
        jsonObject.put("code", StringConverters.null2String(getCode()));
        jsonObject.put("serialNum", StringConverters.null2String(getSerialNum()));
        jsonObject.put("serialCode", StringConverters.null2String(getSerialCode()));
        jsonObject.put("factory", StringConverters.null2String(getFactory()));
        jsonObject.put("type", StringConverters.null2String(getType()));
        jsonObject.put("qrCode", StringConverters.null2String(getQrCode()));
        jsonObject.put("facilityPosition",StringConverters.null2String(getPosition())+ StringConverters.null2String(getPrecisePosition())+StringConverters.null2String(getDetailPosition()));
        jsonObject.put("position", StringConverters.null2String(getPosition()));
        jsonObject.put("precisePosition", StringConverters.null2String(getPrecisePosition()));
        jsonObject.put("detailPosition", StringConverters.null2String(getDetailPosition()));
        jsonObject.put("facilityCatName", getFacilityCat() == null ? "" : getFacilityCat().getCatName());
        jsonObject.put("facilityCatId", getFacilityCat() == null ? null : getFacilityCat().getId());
        jsonObject.put("facilityGroupName", getFacilityGroup() == null ? "" : getFacilityGroup().getFacilityGroupName());
        jsonObject.put("areaName", getArea() == null ? "" : getArea().getAreaName());
        jsonObject.put("areaId", getArea() == null ? null : getArea().getId());
        //现场照片
        if (getFieldPhoto() != null && getFieldPhoto().size() != 0) {
            List<Resource> resourceList = new ArrayList<Resource>(getFieldPhoto());
            jsonObject.put("fieldPhotoPath", StringConverters.null2String(resourceList.get(0).getPath()));
            jsonObject.put("fieldPhotoId", resourceList.get(0).getId());
        }else{
            jsonObject.put("fieldPhotoPath", "");
            jsonObject.put("fieldPhotoId",null);
        }
        //铭牌照片
        if (getPlatePhoto() != null && getPlatePhoto().size() != 0) {
            List<Resource> resourceList = new ArrayList<Resource>(getPlatePhoto());
            jsonObject.put("platePhotoPath", StringConverters.null2String(resourceList.get(0).getPath()));
            jsonObject.put("platePhotoId",resourceList.get(0).getId());
        }else{
            jsonObject.put("platePhotoPath", "");
            jsonObject.put("platePhotoId",null);
        }
        jsonObject.put("isStop", isStop());
        jsonObject.put("isAbnormal", isAbnormal());
        jsonObject.put("power", StringConverters.null2String(getPower()));
        jsonObject.put("mainParm", StringConverters.null2String(getMainParm()));
        jsonObject.put("purpose", StringConverters.null2String(getPurpose()));
        jsonObject.put("upPower", StringConverters.null2String(getUpPower()));
        jsonObject.put("sparePower", StringConverters.null2String(getSparePower()));
        jsonObject.put("releaseDate", CommonSugar.getTypedDefault(DateTimeUtils.formatDateToString(getReleaseDate()), ""));
        jsonObject.put("enableDate", CommonSugar.getTypedDefault(DateTimeUtils.formatDateToString(getEnableDate()), ""));
        jsonObject.put("guarantee", StringConverters.null2String(getGuarantee()));
        jsonObject.put("gridPerson", StringConverters.null2String(getGridPerson()));
        jsonObject.put("organizationName", getOrganization() == null ? "" : getOrganization().getOrganizationName());
        jsonObject.put("organizationId", getOrganization() == null ? null : getOrganization().getId());
        jsonObject.put("longitude", StringConverters.null2String(getLongitude()));
        jsonObject.put("latitude", StringConverters.null2String(getLatitude()));
        jsonObject.put("belongSystem", StringConverters.null2String(getBelongSystem()));
        jsonObject.put("subSystem", StringConverters.null2String(getSubSystem()));
        jsonObject.put("importantDegree", StringConverters.null2String(getImportantDegree()));
        jsonObject.put("parentName", StringConverters.null2String(getParentName()));
        jsonObject.put("costCenter", StringConverters.null2String(getCostCenter()));
        jsonObject.put("faCode", StringConverters.null2String(getFaCode()));
        if(getIndoor()){
            jsonObject.put("indoorText", "是");
        }else{
            jsonObject.put("indoorText", "否");
        }
        if(getEffective()){
            jsonObject.put("effectiveText", "是");
        }else{
            jsonObject.put("effectiveText", "否");
        }
        return jsonObject;
    }
}
